package string_question.leecode.test14;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/5 10:06
 */
public class Solution1 {

    public String longestCommonPrefix(String[] strs) {
        // 1 边界条件判断
        if (strs == null || strs.length == 0) return "";

        // 2 创建一个结果集
        String prefix = strs[0];

        // 3 嵌套循环，外层是数组中的索引，内存遍历的字符串的索引
        for (int i = 1; i < strs.length; i++) {
            int minLen = Math.min(prefix.length(), strs[i].length());
            int j = 0;
            for (; j < minLen; j++) {
                if (prefix.charAt(j) != strs[i].charAt(j)) {
                    break;
                }
            }
            prefix = prefix.substring(0, j);
        }
        return prefix;
    }
}
